package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.Authorities;
import com.daoliuhe.scaffold.model.RoleAuthoritiesRelation;
import com.daoliuhe.scaffold.service.AuthoritiesService;
import com.daoliuhe.scaffold.service.RoleAuthoritiesRelationService;
import com.daoliuhe.scaffold.web.dto.AuthoritiesDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/authorities")
public class AuthoritiesController {

	private static final Logger logger = LoggerFactory.getLogger(AuthoritiesController.class);
	@Autowired
	AuthoritiesService authoritiesService;
	
	@Autowired
	RoleAuthoritiesRelationService roleAuthoritiesRelationService;
	
	@RequestMapping("/list")
	public ModelAndView list(Authorities entity) {
		logger.info("list,entity:{}",entity);
		ModelAndView mav = new ModelAndView("admin/authorities/list");
		mav.addAllObjects(authoritiesService.getPageData(entity));
		mav.addObject("entity", entity);
		return mav;
	}

	/**
	 * @param auth id是编辑的id,其余的字段是查询条件
	 * @param tab
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(Authorities auth, @RequestParam(required=false) String tab, AuthoritiesDto authoritiesDto) {
		logger.info("edit,auth:{},tab:{}, authoritiesDto:{}" ,auth, tab, authoritiesDto);
		ModelAndView mav = new ModelAndView("admin/authorities/edit");
		if (!StringUtils.isEmpty(auth.getId())) {
			Authorities u = authoritiesService.getById(auth.getId());
			u.setPage(auth.getPage());
			u.setRows(auth.getRows());
			mav.addObject("entity", u);
			
			// 用户角色关系分页
			RoleAuthoritiesRelation ur = new RoleAuthoritiesRelation();
			ur.setAuthId(auth.getId());
			ur.setPage(authoritiesDto.getRolePage());
			ur.setRows(authoritiesDto.getRoleRows());
			//接收数据
			mav.addObject("roleMap", roleAuthoritiesRelationService.getRolePageData(ur));
			authoritiesDto.setRolePage(ur.getPage());
			mav.addObject("tab", tab);
		}
		//查询条件
		mav.addObject("param", auth);
		mav.addObject("authoritiesDto", authoritiesDto);
		return mav;
	}

	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Authorities auth) {
		logger.info("saveOrUpdate,auth:{}",auth);
		authoritiesService.saveOrUpdate(auth);
		return "redirect:/authorities/list";
	}

	@RequestMapping("/data")
	@ResponseBody
	public Object data(Authorities auth) {
		logger.info("data,auth:{}",auth);
		return authoritiesService.getPageData(auth);
	}

	@RequestMapping("/vName")
	@ResponseBody
	public Object data(String name, String id) {
		logger.info("data,name:{},id:{}",name,id);
		return authoritiesService.isRightApi(name, id);
	}
	
	@RequestMapping("/vPermission")
	@ResponseBody
	public Object isRightApi(String permission, String id) {
		logger.info("isRightApi,permission:{},id:{}",permission,id);
		return authoritiesService.isRightApi(permission, id);
	}
	
	

}
